package TUILayer;

import ModelLayer.*;
import ControlLayer.*;
import java.util.*;
/**
 * Write a description of class MenuFriends here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MenuFriends
{
    Scanner keyboard = new Scanner(System.in);
    public MenuFriends()
    {
        PersonControls personControl = new PersonControls();
        personControl.createObjects();
    }
    
    public void friendsMenu()
    {
        boolean back = false;
        while(!back)
        {
            int choiseFriends =  writeFriends();
            if(choiseFriends == 1)
            {
                addFriends();
            }
            else
            {
                if(choiseFriends == 2)
                {
                    deleteFriends();
                }
                else
                {
                    if(choiseFriends == 3)
                    {
                        updateFriends();
                    }
                    else
                    {
                        if(choiseFriends == 4)
                        {
                            showFriends();
                        }
                        else
                        {
                            if(choiseFriends == 5)
                            {
                                back = true;
                            }
                            else
                            {
                                System.out.println("Command not valid.");
                            }
                        }
                    }
                }
            }
        }
    }
    
    private int writeFriends()
    {
        System.out.println(" ");
        System.out.println("============================");
        System.out.println("|   FRIENDS SELECTION      |");
        System.out.println("============================");
        System.out.println("| Options:                 |");
        System.out.println("|        1. Add Friends    |");
        System.out.println("|        2. Delete Friends |");
        System.out.println("|        3. Update Friends |");
        System.out.println("|        4. Show Friends   |");
        System.out.println("|        5. Back           |");
        System.out.println("============================");
        System.out.println("     Make your choise: "     );
        
        int choiseFriends = keyboard.nextInt();
        
        return choiseFriends;
    }
    
    private void addFriends()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
        
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                    System.out.println("Input friend's name: ");
                    String name = keyboard.nextLine();
                    System.out.println("Input friend's address: ");
                    String address = keyboard.nextLine();
                    System.out.println("Input friend's postal code(4 symbols): ");
                    String postalCode = keyboard.nextLine();
                    if(postalCode.length() != 4)
                    {
                        System.out.println(" ");
                        System.out.println("Postal Code MUST BE 4 symbols long!");
                        end = true;
                    }
                    else
                    {
                        System.out.println("Input friend's city: ");
                        String city = keyboard.nextLine();
                        System.out.println("Input friend's phone(10 symbols): ");
                        String phone = keyboard.nextLine();
                        if(phone.length() != 10)
                        {
                            System.out.println(" ");
                            System.out.println("Phone number MUST BE 10 symbols long!");
                            end = true;
                        }
                        else
                        {
                            System.out.println("Input friend's ID(6 symbols): ");
                            String id = keyboard.nextLine();
                            if(id.length() != 6)
                            {
                                System.out.println(" ");
                                System.out.println("ID MUST BE 6 symbols long!");
                                end = true;
                            }
                            else
                            {
                                PersonControls personControl = new PersonControls();
                                personControl.createPersons(name, address, postalCode, city, phone, id);
                                System.out.println(" ");
                                System.out.println(personControl.addResult());
                                end = true;
                            }
                        }
                    }
            }
        }
    }
    
    private void deleteFriends()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                    System.out.println("Input friend's ID(6 symbols): ");
                    String id = keyboard.nextLine();
                    if(id.length() != 6)
                    {
                        System.out.println(" ");
                        System.out.println("ID MUST BE 6 symbols long!");
                        end = true;
                    }
                    else
                    {
                        PersonControls personControl = new PersonControls();
                        personControl.deletePersons(id);
                        System.out.println(" ");
                        System.out.println(personControl.returnResult());
                        end = true;
                    }
            }
        }
    }
    
    private void updateFriends()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                System.out.println("Input friend's name: ");
                String newName = keyboard.nextLine();
                System.out.println("Input friend's address: ");
                String newAddress = keyboard.nextLine();
                System.out.println("Input friend's postal code(4 symbols): ");
                String newPostalCode = keyboard.nextLine();
                if(newPostalCode.length() != 4)
                {
                    System.out.println(" ");
                    System.out.println("Postal Code MUST BE 4 symbols long!");
                    end = true;
                }
                else
                {
                    System.out.println("Input friend's city: ");
                    String newCity = keyboard.nextLine();
                    System.out.println("Input friend's phone(10 symbols): ");
                    String newPhone = keyboard.nextLine();
                    if(newPhone.length() != 10)
                    {
                        System.out.println(" ");
                        System.out.println("Phone number MUST BE 10 symbols long!");
                        end = true;
                    }
                    else
                    {
                        System.out.println("Input friend's ID(6 symbols): ");
                        String oldID = keyboard.nextLine();
                        if(oldID.length() != 6)
                        {
                            System.out.println(" ");
                            System.out.println("ID MUST BE 6 symbols long!");
                            end = true;
                        }
                        else
                        {
                             PersonControls personControl = new PersonControls();
                             personControl.updatePersons(newName, newAddress, newPostalCode, newCity, newPhone, oldID);
                             System.out.println(" ");
                             System.out.println("Friend updated.");
                             end = true;
                        }
                    }
                }
            }
        }
    }
    
    private void showFriends()
    {
        boolean end = false;
        while(!end)
        {
            Scanner keyboard = new Scanner(System.in);
            
            System.out.println(" ");
            System.out.println("Do you want to go back? If you do, type 'end', else press Enter.");
            String back = keyboard.nextLine();
            
            if(back.equals("end"))
            {
                end = true;
            }
            else
            {
                    System.out.println("Input friend's ID(6 symbols): ");
                    String id = keyboard.nextLine();
                    if(id.length() != 6)
                    {
                        System.out.println(" ");
                        System.out.println("ID MUST BE 6 symbols long!");
                        end = true;
                    }
                    else
                    {
                        PersonControls personControl = new PersonControls();
                        personControl.showPersons(id);
                        System.out.println(" ");
                        System.out.println(personControl.showPersons(id));
                        end = true;
                    }
            }
        }
    }
}